package ServletMerchant;

import Dao.McdDao;
import Dao.MctDao;
import Entity.Merchandise;
import Entity.Merchant;
import Util.Page;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

@WebServlet(name = "ServletMclist", value = "/merchant")
public class ServletMerchant extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Merchant> list = null;
        String username = request.getParameter("username");

        try {
            list = MctDao.queryByKey("",username);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //获取资产配比
        Double sum = 0.0;
        List<Merchant> Mctlist = null;
        try {
            Mctlist = MctDao.queryByKey("",username);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
//                        ArrayList<Merchant> Mctlist = new ArrayList<>(list);
        for(Merchant merchant:Mctlist){
            ArrayList<Merchandise> ownlist = null;
            try {
                ownlist = McdDao.queryBymctid(merchant.getId());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            Double own = 0.0;
            for (Merchandise merchandise:ownlist){
                own+=merchandise.getPrice()*merchandise.getNum();
            }
            merchant.setW(own);
            sum += own;
        }
        Collections.sort(Mctlist, new Comparator<Merchant>() {
            @Override
            public int compare(Merchant o1, Merchant o2) {
                if (o2.getW()>o1.getW()) return 1;
                else if(o2.getW()==o1.getW()) return 0;
                else return -1;
            }
        });
        if(Mctlist.size()>5) request.setAttribute("Mctlist",Mctlist.subList(0,5));
        else request.setAttribute("Mctlist",Mctlist);
        request.setAttribute("sum",sum);

        //获取页码
        int pagenum = 1,total = list.size();
        if(request.getParameter("pagenum")!=null)
            pagenum = Integer.parseInt(request.getParameter("pagenum"));
        Page page = new Page(pagenum,total);
        request.setAttribute("page",page);
//        System.out.println("get:"+page);
        request.setAttribute("list", list);
        request.setAttribute("username",username);
        request.getRequestDispatcher("merchant.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
